﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;
namespace Travel.Web.N_News
{
    public partial class Modify : Page
    {       

        		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
				{
					int ID=(Convert.ToInt32(Request.Params["id"]));
					ShowInfo(ID);
				}
			}
		}
                private void BindClass()
                {
                    this.ddl_classid.Items.Clear();
                    BLL.NC_NewsClass nc_bll = new BLL.NC_NewsClass();
                    DataSet ds = nc_bll.GetList("1=1");
                    if (ds != null && ds.Tables.Count > 0)
                    {
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            string className = "╋" + dr[1].ToString();
                            string valueID = dr[0].ToString();
                            this.ddl_classid.Items.Add(new ListItem(className, valueID));
                        }
                        //this.ddl_classid.Items.RemoveAt(0);
                    }
                }
			
	private void ShowInfo(int ID)
	{
		Travel.BLL.N_News bll=new Travel.BLL.N_News();
		Travel.Model.N_News model=bll.GetModel(ID);
		this.lblID.Text=model.ID.ToString();
        BindClass();
		this.ddl_classid.SelectedValue=model.N_Classid.ToString();
		this.txtN_Title.Text=model.N_Title;
		this.WebEditor1.Text=model.N_Content;
		this.lb_time.Text=model.N_Time.ToString();
		this.lb_admin.Text=model.N_Admin;
		this.ck_top.Checked=model.N_Istop==1?true:false;
        this.Image1.Src = model.N_Img;
		this.ck_focus.Checked=model.N_Focus==1?true:false;
        this.txt_count.Text = model.N_Count.ToString();
	}

		public void btnSave_Click(object sender, EventArgs e)
		{
			
			string strErr="";
			if(this.txtN_Title.Text.Trim().Length==0)
			{
				strErr+="标题不能为空！\\n";	
			}

            if (!PageValidate.IsNumber(txt_count.Text))
            {
                strErr += "阅读次数必须为整数！\\n";
            }
            
			if(strErr!="")
			{
				MessageBox.Show(this,strErr);
				return;
			}
			int ID=int.Parse(this.lblID.Text);
            int N_Classid =int.Parse( this.ddl_classid.SelectedValue);
			string N_Title=this.txtN_Title.Text;
			string N_Content=this.WebEditor1.Text;
            DateTime N_Time = DateTime.UtcNow;
            string N_Admin = lb_admin.Text;//
            int N_Istop = this.ck_top.Checked ? 1 : 0;
            string N_Img = this.Image1.Src;
            int N_Focus = this.ck_focus.Checked ? 1 : 0;
            int N_count = int.Parse(this.txt_count.Text);


			Travel.Model.N_News model=new Travel.Model.N_News();
			model.ID=ID;
			model.N_Classid=N_Classid;
			model.N_Title=N_Title;
			model.N_Content=N_Content;
			model.N_Time=N_Time;
			model.N_Admin=N_Admin;
			model.N_Istop=N_Istop;
			model.N_Img=N_Img;
			model.N_Focus=N_Focus;
            model.N_Count = N_count;


			Travel.BLL.N_News bll=new Travel.BLL.N_News();
			bll.Update(model);
			Maticsoft.Common.MessageBox.ShowAndRedirect(this,"保存成功！","list.aspx");

		}


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }

        protected void bt_upload_Click(object sender, EventArgs e)
        {
            if (this.uploadImage.FileName == "")
            {
                MessageBox.Show(this, "请选择图片");
            }
            else
            {
                # region 产品图片上传(加水印)
                upload upload = new Maticsoft.Common.upload();
                upload.FileType = "jpg|gif|bmp|jpeg";
                upload.Path = @"\upImage\";
                upload.Sizes = 500;
                string images = "";
                images = upload.SaveAs(Request.Files, images);
                if (images == String.Empty || images == "")
                {
                    // Common.PublicFc.Show(this, "常州云计算提醒您：上传图片失败，请重新添加！");
                    MessageBox.Show(this, "上传图片失败，请重新添加");
                    //Response.Redirect("Product_Add.aspx");
                    return;
                }
                else
                {
                    //string[] array = images.Split('|');
                    //this.Image6.Src = (array.Length - 6 < 0 ? "../images/Nopicture.jpg" : array[array.Length - 5]);
                    //this.Image5.Src = (array.Length - 5 < 0 ? "../images/Nopicture.jpg" : array[array.Length - 4]);
                    //this.Image4.Src = (array.Length - 4 < 0 ? "../images/Nopicture.jpg" : array[array.Length - 3]);
                    //this.Image3.Src = (array.Length - 3 < 0 ? "../images/Nopicture.jpg" : array[array.Length - 2]);
                    //this.Image2.Src = (array.Length - 2 < 0 ? "../images/Nopicture.jpg" : array[array.Length - 1]);
                    this.Image1.Src = images;
                }

                //HttpPostedFile hpf = uploadImage.PostedFile;
                //System.Drawing.Image image = System.Drawing.Image.FromStream(hpf.InputStream, true);
                //string waterName = Server.MapPath("~/watermark.png");
                //LEE.upload.AddImageSignPic(image, upload.saveFilePath, waterName, 5, 100, 6);//图片水印
                //LEE.upload.AddImageSignText(image, upload.saveFilePath,"Lemar drying", 5, 100, "Helvetica", 55);//文字水印
                # endregion

            }
        }
    }
}
